package com.yan.qing.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

/**
 * @description: 线程串行化
 * @author: lf
 * @create: 2021年12月16日 10:24:00
 */
@Service
public class ParallelTest {
    @Autowired
    private Executor service;

    public void parent () {
        /**
         * 3.线程串行化（把后边的线程和前边的串起来）
         */

        //thenRunAsync()无返回值
        CompletableFuture<Void> voidCompletableFuture1 = CompletableFuture.supplyAsync (() -> {
            System.out.println ("线程号为***" + Thread.currentThread ().getId ());
            int i = 5;
            System.out.println ("---------" + i);
            return i;
        }, service).thenRunAsync (() -> {
            System.out.println ("thenRunAsync，不可接受传来的值，自己无返回值的串行化---");
        }, service);

        //thenAccept(x)
        CompletableFuture<Void> voidCompletableFuture2 = CompletableFuture.supplyAsync (() -> {
            System.out.println ("线程号为***" + Thread.currentThread ().getId ());
            int i = 5;
            System.out.println ("---------" + i);
            return i;
        }, service).thenAccept ((r) -> {
            System.out.println ("thenAccept可接受传来的值，自己无返回值的串行化---"+r);
        });

        //thenApply(x)
        CompletableFuture<Integer> voidCompletableFuture3 = CompletableFuture.supplyAsync (() -> {
            System.out.println ("线程号为***" + Thread.currentThread ().getId ());
            int i = 5;
            System.out.println ("---------" + i);
            return i;
        }, service).thenApplyAsync ((r) -> {
            System.out.println ("thenApply可接受传来的值，自己有返回值的串行化---");
            return 10;
        },service);

    }
}